 /**
 * @file tal_zigbee_inner.h
 * @brief Common process - API used for SDK inner
 * @version 1.0.0   
 * @date 2021-09-09
 *
 * @copyright Copyright 2018-2021 Tuya Inc. All Rights Reserved.
 *
 */
#ifndef __TAL_ZIGBEE_INNER_H__
#define __TAL_ZIGBEE_INNER_H__


#ifdef __cplusplus
    extern "C" {
#endif

#include "tkl_network_manager.h"

#include "tal_zcl_ota.h"
#include "tal_time_sync.h"
#include "tal_heartbeat.h"
#include "tal_error_code.h"
#include "tal_data_receive.h"
#include "tal_network_mgr.h"
#include "tal_poll_manager.h"
#include "tal_gw_distinguish.h"
#include "tal_mf_test.h"

/**
 * @brief firmware information write
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_inner_firmware_info_write(VOID_T);

/**
 * @brief check whether the node is joining the network
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return TRUE: node joining in process
 */
BOOL_T tal_zg_inner_joining_in_process(VOID_T);

/**
 * @brief check whether the node is joined the network(tkl_node_joined_network or tkl_node_no_parent) 
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return TRUE: node had joined network
 */
BOOL_T tal_zg_inner_is_joined(VOID_T);

/**
 * @brief zigbee stack init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_stack_init(VOID_T);

/**
 * @brief zigbee network init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_network_init(VOID_T);

/**
 * @brief zigbee remote leave for sdk(will delay 5s to send leave)
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_inner_leave_for_sdk(VOID_T);

/**
 * @brief zigbee mf test leave for gateway mf test
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_inner_leave_for_mf_test(VOID_T);

/**
 * @brief receive data init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_data_receive_init(VOID_T);

/**
 * @brief send data init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_data_send_init(VOID_T);

/**
 * @brief zigbee end-device rejoin init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   rejoin_cfg: point to rejoin configuration
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_rejoin_init(TAL_ZG_REJOIN_CFG_T *rejoin_cfg);

/**
 * @brief zigbee end-device scan for rejoin
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   rejoin_cfg: point to rejoin configuration
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
VOID_T tal_zg_inner_end_device_scan_for_rejoin(VOID_T);

/**
 * @brief zigbee end-device network channel callback
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   state: network state
 * @return none
 */
VOID_T tal_zg_inner_end_device_nwk_change_callback(TKL_NWK_BASIC_STATE_E state);

/**
 * @brief zigbee end-device polling init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   node:     node type
 * @param[in]   poll_cfg: poll config
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_poll_init(ZG_NODE_TYPE_E node, TAL_ZG_POLL_CFG_T *poll_cfg);

/**
 * @brief clear the control parameters of poll
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_inner_poll_clear(VOID_T);

/**
 * @brief force stop poll use for sleep end-device
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T ta_zg_inner_poll_force_stop(VOID_T);

/**
 * @brief get polling interval
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
UINT16_T tal_zg_inner_poll_interval_get(VOID_T);

/**
 * @brief follow poll after data send for more data
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_inner_poll_start_for_data(VOID_T);

/**
 * @brief follow poll after join for data
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_inner_poll_start_for_join(VOID_T);

/**
 * @brief follow poll after rejoin for data
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_inner_poll_start_for_rejoin(VOID_T);

/**
 * @brief gateway remotely controls the behavior of the local poll of the device
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   interval_ms: polling interval
 * @param[in]   timeout_s:   polling timeout
 * @return none
 */
VOID_T tal_zg_inner_poll_start_with_gw_control(UINT16_T interval_ms, UINT16_T timeout_s);


/**
 * @brief zigbee group init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_group_init(VOID_T);

/**
 * @brief zigbee scene init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_scene_init(VOID_T);

/**
 * @brief loade gateway type from nv flash
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_inner_joined_gateway_type_load(VOID_T);

/**
 * @brief save gateway type to nv flash
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   type: gateway type
 * @return none
 */
VOID_T tal_zg_inner_joined_gateway_type_save(TAL_ZG_GW_TYPE_T type);

/**
 * @brief zigbee ota init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   app_ver: application version
 * @param[in]   manuf_id: manufacturer id
 * @param[in]   image_type: ota image type
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_inner_ota_init(UINT_T app_ver, UINT16_T manuf_id, UINT16_T image_type);

/**
 * @brief time synchronization init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   cfg: pointe to time sync config
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_inner_time_sync_init(TAL_TIME_SYNC_T *cfg);

/**
 * @brief stop time cycle synchronization
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   none
 *
 * @return none
 */
VOID_T tal_inner_time_sync_stop(VOID_T);

/**
 * @brief wait for a while to start cycle time synchronization 
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   time_ms: delay ms
 * @return none
 */
VOID_T tal_inner_time_sync_start_with_delay(UINT_T time_ms);

/**
 * @brief time synchronization data received from gateway
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   time_sec: time synchronization (bat: sec)
 * @return none
 */
VOID_T tal_inner_time_sync_receive_handle(UINT_T time_sec);


/**
 * @brief heartbeat send init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   cfg: pointe to heartbeat config
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_inner_heartbeat_init(TAL_HEARTBEAT_T *cfg);

/**
 * @brief wait for a while to start cycle send heartbeat
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   delay_ms: delay time
 *
 * @return none
 */
VOID_T tal_inner_heartbeat_start_with_delay(UINT_T delay_ms);

/**
 * @brief error code send init
 * NOTE: this API is limited to SDK internal calls
 *
 * @param[in]   cfg: pointe to error code config
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_inner_error_code_init(TAL_ERR_CODE_T *cfg);

/**
 * @brief wait for a while to start cycle send error code
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   delay_ms: delay time
 *
 * @return none
 */
VOID_T tal_inner_error_code_start_with_delay(UINT_T delay_ms);

/**
 * @brief mf test init
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   exit_cb:   point to mf test exit callback
 * @param[in]   handle_cb: point to mf test item handle function after entering the mf test
 *
 * @return none
 */
VOID_T tal_inner_mf_test_init(TUYA_TEST_EXIT_CB_T exit_cb, TUYA_TEST_HANDLE_CB_T handle_cb);

/**
 * @brief check mf test is doing
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[out]   src: get mf test source
 *
 * @return TRUE: mf test is doing
 */
BOOL_T tal_inner_mf_test_is_doing(TUYA_MF_TEST_SRC_T *src);

/**
 * @brief mf test disable
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   none
 *
 * @return TRUE: mf test is doing
 */
VOID_T tal_inner_mf_test_disable(VOID_T);


typedef VOID_T (*TAL_CLUSTER_CB)(TAL_ZCL_MSG_T *msg);
/**
 * @brief private cluster init
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   none
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_inner_private_cluster_init(VOID_T);

/**
 * @brief private cluster receive callback
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   cluster: cluster id
 * @param[in]   msg: pointe to message
 *
 * @return none
 */
VOID_T tal_zg_inner_private_cluster_receive_callback(UINT16_T cluster, TAL_ZCL_MSG_T* msg);

/**
 * @brief save attribute to nv flash
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   ep:         endpoint id
 * @param[in]   cluster:    cluster id
 * @param[in]   attr:       attribute id
 * @param[in]   attr_value: point to attribute data
 * @param[in]   attr_len:   attribute length
 *
 * @return none
 */
BOOL_T tal_inner_attr_nv_save(UINT8_T ep, UINT16_T cluster, UINT16_T attr, UINT8_T *attr_value, UINT8_T attr_len);

/**
 * @brief load attribute from nv flash
 * NOTE: this API is limited to SDK internal calls
 * 
 * @param[in]   ep:         endpoint id
 * @param[in]   cluster:    cluster id
 * @param[in]   attr:       attribute id
 * @param[out]  attr_value: point to attribute data
 * @param[in]   attr_len:   attribute length
 * @return none
 */
BOOL_T tal_inner_attr_nv_load(UINT8_T ep, UINT16_T cluster, UINT16_T attr, UINT8_T *attr_value, UINT8_T attr_len);


#ifdef __cplusplus
}
#endif

#endif

